#include "front/text_normalize.h"

using namespace std;

namespace ppspeech {

TextNormalizer::TextNormalizer()
{

}

TextNormalizer::~TextNormalizer()
{

}


//替换
void TextNormalizer::WstrReplacePosition(wstring* fpInSentence, const int fcInPosition, const int fcInLength, const wstring& fcpInWstrReplace)
{
	fpInSentence->erase(fcInPosition, fcInLength);			//删除原来的
	fpInSentence->insert(fcInPosition, fcpInWstrReplace);	//插入新的
}


//根据标点符号切分句子
vector<wstring> TextNormalizer::WstrSplitByPunctuation(const wstring& fcpInSentence)
{
	const wregex mcWregexPunctuation(L"[，；。？！,;?!]");
	vector<wstring> tVecSplitParts;
	wstring tTempSentence = fcpInSentence;
	wsmatch tWsmatch;

	while (regex_search(tTempSentence, tWsmatch, mcWregexPunctuation)) {
		tVecSplitParts.push_back( tTempSentence.substr(0, tWsmatch.position(0) + tWsmatch.length(0)) );
		WstrReplacePosition(&tTempSentence, 0, tWsmatch.position(0) + tWsmatch.length(0), L"");
	}
	//如果最后没有标点符号
	if (tTempSentence != L"") {
		tTempSentence += L"。";	//强行补充末尾标点
		tVecSplitParts.push_back(tTempSentence);
	}

	//------ 合并句子 ------
	vector<wstring> tVecMergeParts;
	wstring tMergeLast = L"";
	for(size_t i=0; i<tVecSplitParts.size(); i++) {
		const wstring tCurrPart = tVecSplitParts[i];
		const wstring tNextPart = (i+1 < tVecSplitParts.size()) ? tVecSplitParts[i+1] : L"";

		if(tCurrPart.length() >= 2 && tNextPart.length() > 0) {
			const wstring tCurrPartBack1 = tCurrPart.substr(tCurrPart.length()-1, 1);
			const wstring tCurrPartBack2 = tCurrPart.substr(tCurrPart.length()-2, 1);
			const int tNextPartFirstNumberPosition = GetWstrFirstNumberPosition(tNextPart);	//下个字句的数字出现在前两位
			if(tCurrPartBack1 == L"," && IsWstrAllNumber(tCurrPartBack2) && tNextPartFirstNumberPosition >= 0 && tNextPartFirstNumberPosition <= 1) {
				tMergeLast += tCurrPart;
				continue;
			}
		}
		tVecMergeParts.push_back( tMergeLast + tCurrPart );
		tMergeLast = L"";
	}

	if(tMergeLast != L"") {
		tVecMergeParts.push_back( tMergeLast );
	}

	return tVecMergeParts;
}


//整体正则，按顺序
void TextNormalizer::SentenceNormalize(wstring* fpInWsentence)
{
	(*fpInWsentence) = L" " + (*fpInWsentence);

	TnReWebsite(fpInWsentence);

	TnReScientificNotation(fpInWsentence, mUseLanguage);	//科学计数法

	TnReInitialSymbol(fpInWsentence, mUseLanguage);			//符号替换

	TnReLetterAa(fpInWsentence);
	TnReEnglishPost(fpInWsentence);

	TnReLatitudeLongitude(fpInWsentence, mUseLanguage);		//经纬度
	TnReTrigonometric(fpInWsentence, mUseLanguage);			//三角函数

	TnReDotNumber(fpInWsentence, mUseLanguage);				//数字点数字点数字
	TnReDotLetter(fpInWsentence, mUseLanguage);				//英文加小数

	TnReCurrency(fpInWsentence, mUseLanguage);
	TnReEnAmPm(fpInWsentence, mUseLanguage);

	TnReDashLetter(fpInWsentence, mUseLanguage);			//横杠带字母

	TnReLetterDecimal(fpInWsentence, mUseLanguage);			//字母加小数
	TnReNumberLetter(fpInWsentence, true, mUseLanguage);	//整数字母（&扩展小数负数）
	TnReDecimalLetter(fpInWsentence, mUseLanguage);			//小数加字母
	TnReLetterNumber(fpInWsentence, mUseLanguage);			//字母整数

	TnReLetterSplit(fpInWsentence);

	TnReNo2Number(fpInWsentence, mUseLanguage);

	TnReDashColon(fpInWsentence, mUseLanguage);			//横杠带冒号

	TnReColon(fpInWsentence, mUseLanguage);				//冒号
	TnReComma(fpInWsentence, mUseLanguage);				//逗号

	TnReDashDecimal(fpInWsentence, mUseLanguage);		//横杠带小数
	TnReDash(fpInWsentence, mUseLanguage);				//横杠

	TnReSlashUnit(fpInWsentence, mUseLanguage);			//斜杠带单位
	TnReSlash(fpInWsentence, mUseLanguage);				//斜杠

	TnRePercentage(fpInWsentence, mUseLanguage);		//百分号

	TnReWavyline(fpInWsentence, mUseLanguage);			//波浪线
	
	TnReChineseCentury(fpInWsentence, mUseLanguage);	//世纪年代
	TnReChineseDate(fpInWsentence, mUseLanguage);		//中文日期

	TnReZhQuantifiers(fpInWsentence, mUseLanguage);		//num+中文量词

	TnReNegDecNum(fpInWsentence, false, mUseLanguage);	//小数负数

	TnReMobilePhone(fpInWsentence, mUseLanguage);		//手机号码
	TnReLandlinePhone(fpInWsentence, mUseLanguage);		//座机号码
	TnReOtherPhone(fpInWsentence, mUseLanguage);		//电话/号码+num

	TnReChinesePost(fpInWsentence, mUseLanguage);

	TnReEnDate(fpInWsentence, mUseLanguage);

	TnReEnNumAndNum(fpInWsentence);
	TnReEnAtNumber(fpInWsentence, mUseLanguage);

	TnReNegDecNum(fpInWsentence, true, mUseLanguage);

	TnReFinallySymbol(fpInWsentence, mUseLanguage);		//符号替换
}
}